﻿#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.InformationSecurity.CISF.Common.Utilities;
using CISFAppDeployer;
using System.IO;
using System;
#endregion

namespace SecurityPortal.Test.UnitTests
{
    /// <summary>
    ///This is a test class for UtilityTest and is intended
    ///to contain all UtilityTest Unit Tests
    ///</summary>
    [TestClass()]
    public class UtilityTest
    {
        private TestContext testContextInstance;
        static string serverName = "Server_Name"; // TODO: Initialize to an appropriate value
        static string siteName = "Default Web Site"; // TODO: Initialize to an appropriate value
        static string folderName = "Application_Folder_Name"; // TODO: Initialize to an appropriate value
        string appName = "Application_Name"; // TODO: Initialize to an appropriate value

        string portalArchiveFolder = @"\\FileShare\Archive";
        static string portalRootFolder = @"\\FileShare\wwwroot\"; // TODO: Initialize to an appropriate value
        static string applicationPath = @"/Applications/"; // TODO: Initialize to an appropriate value
        string applicationPool = "AppPool_Name"; // TODO: Initialize to an appropriate value
        static string destinationFolder = string.Empty;

        static string sourceFolder = @"C:\ProjectSourceFolderName\"; // TODO: Initialize to an appropriate value
        static string applicationsDirectory;
        string strAppPoolActName = "DOMAIN\\USERNAME"; // TODO: Initialize to an appropriate value
        string strAppPoolActPassword = "XYZ123"; // TODO: Initialize to an appropriate value
        static IISAdmin iisAdmin = null;
        bool IsCustomAppPoolAccount = true; // TODO: Initialize to an appropriate value
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
             
            applicationPath += folderName;
            applicationsDirectory = portalRootFolder + "Applications\\";
            destinationFolder = applicationsDirectory + folderName; 
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //    
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetUserAlias with EMPTY username
        ///</summary>
        [TestMethod()]
        public void GetUserAliasEmptyTest()
        {
            string strUserName = ""; // TODO: Initialize to an appropriate value
            string expected = "Error: strUserName is null or empty"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUserAlias(strUserName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetUserAlias
        ///</summary>
        [TestMethod()]
        public void GetUserAliasTest()
        {
            string strUserName = "DOMAIN\\USER"; // TODO: Initialize to an appropriate value
            string expected = "USERNAME"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUserAlias(strUserName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetUserDomain with Empty name
        ///</summary>
        [TestMethod()]
        public void GetUserDomainEmptyTest()
        {
            string strUserName = ""; // TODO: Initialize to an appropriate value
            string expected = "Error: strUserName is null or empty"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUserDomain(strUserName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetUserDomain
        ///</summary>
        [TestMethod()]
        public void GetUserDomainTest()
        {
            string strUserName = "DOMAIN\\USER"; // TODO: Initialize to an appropriate value
            string expected = "DOMAIN"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUserDomain(strUserName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetUsername
        ///</summary>
        [TestMethod()]
        public void GetUsernameTest()
        {
            string expected = "DOMAIN\\USER"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUsername();
            Assert.AreEqual(expected, actual);

        }

        /// <summary>
        ///A test for GetUserFullName
        ///</summary>
        [TestMethod()]
        public void GetUserFullNameTest()
        {
            string expected = "USER FULL NAME"; // TODO: Initialize to an appropriate value
            string actual;
            actual = Utility.GetUserFullName("DOMAIN\\USER");
            Assert.AreEqual(expected, actual);
        }
        [TestMethod()]
        public void AddAppDirectoryTest()     
        {          
            
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.AddAppDirectory(applicationsDirectory, folderName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CopyAppDirectory
        ///</summary>
        [TestMethod()]
        public void CopyAppDirectoryTest()
        {
             // TODO: Initialize to an appropriate value
            bool overwrite = true; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.CopyAppDirectory(sourceFolder, destinationFolder, overwrite);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for DeleteApplication
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void DeleteApplicationTest()
        {
            FileSystemInfo destFilesys = new DirectoryInfo(destinationFolder); // TODO: Initialize to an appropriate value
            Installer_Accessor.DeleteApplication(destFilesys);
        }

        /// <summary>
        ///A test for RemoveAppDirectory
        ///</summary>
        [TestMethod()]
        public void RemoveAppDirectoryTest()
        {
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.RemoveAppDirectory(applicationsDirectory, folderName, portalArchiveFolder);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UpdateNewAppSettings
        ///</summary>
        [TestMethod()]
        public void UpdateNewAppSettingsTest()
        {
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.UpdateNewAppSettings(appName, folderName, destinationFolder);
            Assert.AreEqual(expected, actual);
        }
        
        /// <summary>
        ///A test for UpdatePortalSitemap
        ///</summary>
        [TestMethod()]
        public void UpdatePortalSitemapTest()
        {
            bool installFlag = true; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.UpdatePortalSitemap(portalRootFolder, folderName, installFlag);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void UpdatePortalSitemapUninstallTest()
        {
            bool installFlag = false; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = Installer.UpdatePortalSitemap(portalRootFolder, folderName, installFlag);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod()]
        public void AddApplicationTest()
        {                       
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            iisAdmin = new IISAdmin(serverName, siteName);   
            actual = iisAdmin.AddApplication(applicationPath, applicationPool, destinationFolder);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CreateApplicationPool
        ///</summary>
        [TestMethod()]        
        public void CreateApplicationPoolTest()
        {          
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            iisAdmin = new IISAdmin(serverName, siteName);   
            actual = iisAdmin.CreateApplicationPool(applicationPool, IsCustomAppPoolAccount, strAppPoolActName, strAppPoolActPassword);
            Assert.AreEqual(expected, actual);
        }             


        /// <summary>
        ///A test for RemoveApplication
        ///</summary>
        [TestMethod()]
        public void RemoveApplicationTest()
        {
            string applicationPath = @"/Applications/" + folderName; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            iisAdmin = new IISAdmin(serverName, siteName);   
            actual = iisAdmin.RemoveApplication(applicationPath);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UpdateApplicationSitemaps
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void UpdateApplicationSitemapsTest()
        {
            bool installFlag = true; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            iisAdmin = new IISAdmin(serverName, siteName);   
            actual = iisAdmin.UpdateApplicationSitemaps(folderName, applicationsDirectory, installFlag);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UpdateApplicationSitemaps
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void UpdateApplicationSitemapsUninstallTest()
        {
            bool installFlag = false; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            iisAdmin = new IISAdmin(serverName, siteName);
            actual = iisAdmin.UpdateApplicationSitemaps(folderName, applicationsDirectory, installFlag);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///A test for UpdateCurrentApplicationSitemap
        ///</summary>
        [TestMethod()]
        public void UpdateCurrentApplicationSitemapTest()
        {
            iisAdmin = new IISAdmin(serverName, siteName);   
            iisAdmin.UpdateCurrentApplicationSitemap(destinationFolder, appName);
        }

        /// <summary>
        ///A test for AppDeploySteps
        ///</summary>
        [TestMethod()]
        public void AppDeployStepsTest()
        {
            bool installFlag = true; // TODO: Initialize to an appropriate value
            int expected = 0; // TODO: Initialize to an appropriate value
            int actual;
            actual = AppDeployer.ApplicationDeploymentSteps(installFlag);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsValidUser
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void IsValidUserTest()
        {
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = AppDeployer_Accessor.IsValidUser(strAppPoolActName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for LoadConfiguration
        ///</summary>
        [TestMethod()]
        public void LoadConfigurationTest()
        {
            string configFile = "C:\\PortalApp.config"; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = AppDeployer.LoadConfiguration(configFile);
            Assert.AreEqual(expected, actual);
        }

               /// <summary>
        ///A test for Main
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void MainTest()
        {
            string[] args = { "/i", "C:\\PortalApp.config", "DOMAIN\\USER", "compaq1-2" }; // TODO: Initialize to an appropriate value
            int expected = 0; // TODO: Initialize to an appropriate value
            int actual;
            actual = AppDeployer.Main(args);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ValidateArguments
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CISFAppDeployer.exe")]
        public void ValidateArgumentsTest()
        {
            string[] args = { "/i", "C:\\PortalApp.config", "DOMAIN\\USER", "compaq1-2" }; // TODO: Initialize to an appropriate value
            int expected = 0; // TODO: Initialize to an appropriate value
            int actual;
            actual = AppDeployer_Accessor.ValidateArguments(args);
            Assert.AreEqual(expected, actual);
        }
    }
}
